package wimm.namespace;

import com.wimm.framework.app.LauncherActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ToolkitWatchesActivity extends LauncherActivity implements SensorEventListener {
    
	private SensorManager mySensorManager;
	private TextView xView, yView, zView;
	private long saveMS;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
        mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        saveMS = System.currentTimeMillis();
        
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.SCREEN_DIM");
        filter.addAction("android.intent.action.SCREEN_ON");
        this.registerReceiver(mReceiver, filter);
        
        xView = (TextView)findViewById(R.id.x_view);
        yView = (TextView)findViewById(R.id.y_view);
        zView = (TextView)findViewById(R.id.z_view);
        
        parent.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent ev) {
            	if (ev.getAction() == MotionEvent.ACTION_DOWN ){
            		long currentMS = System.currentTimeMillis();
            		xView.setText("x at " + ev.getX());
            		yView.setText("y at " + ev.getY());
            		zView.setText("time:" + (currentMS-saveMS));
            		saveMS = currentMS;
            	}
            	return true;
            }
        });
    }
    
    private void setAccelerometerUpdatesEnabled(boolean enabled) {
        if (enabled) {
            mySensorManager.registerListener(this,
                    mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                    SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            mySensorManager.unregisterListener(this);
        }
    }
    
    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        	float[] values = event.values;
        	//String str = values[1] + "";
            xView.setText(Float.toString(values[0]));
            yView.setText(Float.toString(values[1]));
            zView.setText(Float.toString(values[2]));
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        setAccelerometerUpdatesEnabled(true);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        setAccelerometerUpdatesEnabled(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
    
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("android.intent.action.SCREEN_DIM")) {
                setAccelerometerUpdatesEnabled(false);
            } else if (action.equals("android.intent.action.SCREEN_ON")) {
                setAccelerometerUpdatesEnabled(true);
            }
        }
    };
}